package gob.cgr.web.sgrh.servlet;

import gob.cgr.sgrh.ejb.entidad.Legajo;
import gob.cgr.web.sgrh.service.SigerhService;
import gob.cgr.web.sgrh.util.Utilitario;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URL;

import javax.inject.Inject;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class LegajoManagerServlet
 */
@WebServlet("/LegajoManagerServlet")
public class LegajoManagerServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	@Inject
	SigerhService service;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LegajoManagerServlet() {
        super();
        // TODO Auto-generated constructor stub
    }
    
    private void performTask(HttpServletRequest request, HttpServletResponse response) throws ServletException,
	IOException {
    	System.out.println("llego al servlet :::" +request.getParameter("legajo"));
    	String idLegajo=request.getParameter("legajo");
    	
    	
    	Legajo legajo=service.obtenerLegajo(Integer.valueOf(idLegajo));
    	System.out.println("el legajo es "+legajo);
    	
    	//String fileName=Utilitario.getCarpetaLegajo()+legajo.getIdPersonal().getCodigoPersonal()+"/"+legajo.getArchivo();
    	String urlResource=Utilitario.HOST_LEGAJO+"/sigerhdata/legajos/"+legajo.getIdPersonal().getCodigoPersonal()+"/"+legajo.getArchivo();
    	System.out.println(urlResource);
    	URL f;
    	
    	try {
    		f= new URL(urlResource);
    		System.out.println("escribiendo de servlet");
    		if(urlResource.toLowerCase().lastIndexOf("pdf")!=-1)
    		response.setContentType("application/pdf");
    		else
    		response.setContentType("image/jpeg");
    		response.setHeader("Content-Disposition", "inline;filename="+legajo.getArchivo());
    		InputStream fis = f.openStream();
    		BufferedInputStream bis = new BufferedInputStream(fis);
    		ServletOutputStream sos = response.getOutputStream();
    		byte[] buffer = new byte[(int) fis.available()];
    		while (true) {
    		   int bytesRead = bis.read(buffer, 0, buffer.length);
    		   if (bytesRead < 0) {
    		      break;
    		   }
    		   sos.write(buffer, 0, bytesRead);
    		}
    		sos.flush();
    		fis.close();
    		
		} catch (Exception e) {
			// TODO: handle exception
			response.setContentType("text/html");
			response.getWriter().write("Ocurrio un error al leer el archivo legajo "+e.getMessage());
		}
		
    }
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		
		performTask(request, response);
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		performTask(request, response);
	}

}
